package com.ruoyi.lvlian.domain;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;

/**
 * 主体基本信息对象 lv_business_license_info
 *
 * @author lvlian
 * @date 2024-07-24
 */
@Data
@ToString
public class BusinessLicenseInfo {
    private static final long serialVersionUID = 1L;

    private String id;

    @ApiModelProperty(value = "主体编号-编号规则中包含上类型")
    private String subjectNum;

    @ExcelProperty("主体类型")
    @ApiModelProperty(value = "主体类型:自然人, 企业")
    private String subjectType;

    @ExcelProperty("主体角色")
    @ApiModelProperty(value = "主体角色 供=应商、利废企业、再生资源企业")
    private String subjectRole;

    @ExcelProperty("名称")
    @ApiModelProperty(value = "名称")
    private String companyName;


    @ExcelProperty("简称")
    @ApiModelProperty(value = "简称")
    private String companyShortName;

    @ExcelProperty("企业统一社会信用代码")
    @ApiModelProperty(value = "统一社会信用代码-税号")
    private String companyCode;

    @ExcelProperty("工商类型")
    @ApiModelProperty(value = "工商类型")
    private String companyType;

    @ExcelProperty("注册资本")
    @ApiModelProperty(value = "注册资本")
    private String registeredCapital;

    @ExcelProperty("法人名称")
    @ApiModelProperty(value = "法人姓名")
    private String legalName;

    @ExcelProperty("固定电话")
    @ApiModelProperty(value = "固定电话")
    private String legalPhone;

    @ExcelProperty("法人证件类型")
    @ApiModelProperty(value = "法人证件类型")
    private String legalIdType;

    @ExcelProperty("法人证件号码")
    @ApiModelProperty(value = "法人证件号")
    private String legalIdCard;

    @ExcelProperty("成立日期")
    @ApiModelProperty(value = "成立日期")
    private String registeredDate;


    @ExcelProperty("经营范围")
    @ApiModelProperty(value = "经营范围")
    private String businessScope;

    @ExcelProperty("登记日期")
    @ApiModelProperty(value = "登记日期")
    private String approvedDate;


    @ExcelProperty("营业期限")
    @ApiModelProperty(value = "营业期限 ")
    private String expiryDate;


    @ExcelProperty("登记机关")
    @ApiModelProperty(value = "登记机关发证机关   ")
    private String agency;

    @ExcelProperty("注册地址")
    @ApiModelProperty(value = "注册地址")
    private String registeredAddr;

    @ExcelProperty("联系电话")
    @ApiModelProperty(value = "联系电话")
    private String contactPersonPhone;

    @ExcelProperty("联系人性别")
    @ApiModelProperty(value = "联系人性别")
    private String contactGender;


    @ExcelProperty("联系人名称")
    @ApiModelProperty(value = "联系人名称")
    private String contactPerson;


    @ExcelProperty("开户银行")
    @ApiModelProperty(value = "开户银行")
    private String openBank;

    @ExcelProperty("开户行账号")
    @ApiModelProperty(value = "开户行账号")
    private String bankAccno;

    @ApiModelProperty(value = "认证状态 1 已认证 2 待认证 3 认证中 4认证失败")
    private String rzStatus;

    @ApiModelProperty(value = "行政区划代码")
    private String xzqhCode;

    @ApiModelProperty(value = "业务负责人名称")
    private String salePersonName;

    @ApiModelProperty(value = "业务负责人联系方式")
    private String salePersonPhone;

    @ApiModelProperty(value = "当前状态 如=运营中、停业等")
    private String companyStatus;


    /**
     * 录入人
     */
    @ApiModelProperty(value = "录入人")
    private String createUser;

    /**
     * 录入日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private String createDate;

    @ExcelProperty("所属公司编码")
    @ApiModelProperty(value = "")
    private String orgCode;

    @ApiModelProperty(value = "天眼评分")
    private Integer tianyanScore;


    @ApiModelProperty(value = "供应商标签")
    private String label;
    @ApiModelProperty(value = "场地位置1")
    private String standardAddress1;
    @ApiModelProperty(value = "场地位置2")
    private String standardAddress2;
    @ApiModelProperty(value = "门牌号码")
    private String standardAddress3;


    @ApiModelProperty(value = "场地位置经度")
    private Double standardLng;

    @ApiModelProperty(value = "场地位置纬度")
    private Double standardLat;

    @ApiModelProperty(value = "数据来源填 实时物流小程序  四流平台")
    private String dataSource;


    @ApiModelProperty(value = "营业执照照片")
    private String companyImg;


    @ApiModelProperty(value = "其他补充图片")
    private String otherImg;

    @ApiModelProperty(value = "法人身份证照片,自然人的身份证正面照片")
    private String idCardImg;
    @ApiModelProperty(value = "银行卡图片")
    private String bankImg;
    @ApiModelProperty(value = "开票资料图片")
    private String invoiceImg;
    @ApiModelProperty(value = "法人身份证照片反面,自然人的身份证反面照")
    private String idCardBackImg;


    public String getInvoiceImg() {
        if (StringUtils.isNotEmpty(invoiceImg) && !invoiceImg.contains("http")) {

            return "https://data.lvlianhengsheng.com/dev-api/file/fileAttachmentInfo/viewImage?key=" + invoiceImg;
        }
        return invoiceImg;
    }

    public String getIdCardBackImg() {
        if (StringUtils.isNotEmpty(idCardBackImg) && !idCardBackImg.contains("http")) {

            return "https://data.lvlianhengsheng.com/dev-api/file/fileAttachmentInfo/viewImage?key=" + idCardBackImg;
        }
        return idCardBackImg;
    }

    public String getCompanyImg() {
        if (StringUtils.isNotEmpty(companyImg) && !companyImg.contains("http")) {

            return "https://data.lvlianhengsheng.com/dev-api/file/fileAttachmentInfo/viewImage?key=" + companyImg;
        }
        return companyImg;
    }


    public String getOtherImg() {
        if (StringUtils.isNotEmpty(otherImg) && !otherImg.contains("http")) {

            return "https://data.lvlianhengsheng.com/dev-api/file/fileAttachmentInfo/viewImage?key=" + otherImg;
        }
        return otherImg;
    }


    public String getIdCardImg() {
        if (StringUtils.isNotEmpty(idCardImg) && !idCardImg.contains("http")) {

            return "https://data.lvlianhengsheng.com/dev-api/file/fileAttachmentInfo/viewImage?key=" + idCardImg;
        }
        return idCardImg;
    }

    public String getBankImg() {
        if ("https://data.lvlianhengsheng.com/dev-api".equalsIgnoreCase(bankImg) || org.apache.commons.lang3.StringUtils.countMatches(bankImg, "http") > 1) {
            return null;
        }
        if (com.ruoyi.common.core.utils.StringUtils.isNotEmpty(bankImg) && !bankImg.contains("http")) {
            return "https://data.lvlianhengsheng.com/dev-api/file/fileAttachmentInfo/viewImage?key=" + bankImg;
        }

        return bankImg;
    }

    public String getRegisteredDate() {
        if (StringUtils.isNotEmpty(registeredDate)) {
            return DateUtils.unifyDateFormats(registeredDate);
        }
        return registeredDate;
    }


    public String getExpiryDate() {
        if (StringUtils.isNotEmpty(expiryDate)) {
            return DateUtils.unifyDateFormats(expiryDate);
        }

        return expiryDate;
    }

    public String getApprovedDate() {
        if (StringUtils.isNotEmpty(approvedDate)) {
            return DateUtils.unifyDateFormats(approvedDate);
        }
        return approvedDate;
    }

    //自然人信息
    @ExcelProperty("户籍地址")
    @ApiModelProperty(value = "户籍地址")
    private String address;

    @ExcelProperty("证件类型")
    @ApiModelProperty(value = "证件类型")
    private String idType;

    @ExcelProperty("证件号码")
    @ApiModelProperty(value = "自然人证件号码")
    private String idNum;

    @ApiModelProperty(value = "领域父id")
    private String domainParentId;


    @ApiModelProperty(value = "领域父mame")
    private String domainParentName;

    /**
     * 邮箱
     */
    @ApiModelProperty(value = "邮箱")
    private String email;

    /**
     * 供应商状态
     */
    @ApiModelProperty(value = "供应商状态")
    private String status;

    @ApiModelProperty(value = "备注remark")
    private String remark;

    @ApiModelProperty(value = "收款人姓名")
    private String payeeName;
    @ApiModelProperty(value = "收款人手机号码")
    private String payeePhone;

    public String getContactGender() {
        if (idNum != null && !idNum.isEmpty()) {
            // 身份证长度应为18位（新身份证）或15位（旧身份证）
            if (idNum.length() == 18) {
                // 第17位是性别位（索引16，因为从0开始）
                char genderCode = idNum.charAt(16);
                return (genderCode % 2 == 1) ? "男" : "女";
            } else if (idNum.length() == 15) {
                // 15位身份证，第15位是性别位（索引14）
                char genderCode = idNum.charAt(14);
                return (genderCode % 2 == 1) ? "男" : "女";
            }
        }
        return contactGender; // 如果身份证为空或格式不正确，返回原来的gender值
    }

    @ExcelProperty("出生日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "出生日期", example = "2020-01-01", dataType = "string")
    private String birthDate;
    public String getBirthDate() {
        if (idNum == null || idNum.length() != 18) {
            return birthDate;
        }

        try {
            String birthDateStr = idNum.substring(6, 14);

            // 验证日期是否有效
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            sdf.setLenient(false); // 严格模式，验证日期有效性
            Date date = sdf.parse(birthDateStr);

            // 格式化为标准日期格式
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
            return outputFormat.format(date);
        } catch (Exception e) {
            return birthDate; // 日期格式无效时返回原值
        }
    }

}
